﻿$(document).ready(function () {
    var load = function (page) {
        var name = $("#txtName").val();
        $.ajax({
            type: "POST",
            url: "/User/Search",
            data: { page: page, name: name },
            dataType: "json",
            success: function (response) {
                var lst = response.data;
                var str = "";
                str += "<!-- INICIO: BLOCO_TABELA -->";
                str += "<table width='100%' border='0' cellpadding='0' cellspacing='2' class='texto10azulclaro'>";
                str += "<tr class='texto11branco' style='background:url(../../content/redmond/images/ui-bg_gloss-wave_55_5c9ccc_500x100.png);'>";
                str += "<td height='25' width='5%'>";
                str += "&nbsp;";
                str += "</td>";
                str += "<td width='20%'>";
                str += "<strong>&nbsp;Nome</strong>";
                str += "</td>";
                str += "<td width='20%'>";
                str += "<strong>&nbsp;Login</strong>";
                str += "</td>";
                str += "<td width='25%'>";
                str += "<strong>&nbsp;E-mail</strong>";
                str += "</td>";
                str += "<td width='20%'>";
                str += "<strong>&nbsp;Cidade/UF</strong>";
                str += "</td>";
                str += "<td width='10%' align='center'>";
                str += "<strong>A&ccedil;&otilde;es</strong>";
                str += "</td>";
                str += "</tr>";
                if (lst.length > 0) {
                    for (var i = 0; i < lst.length; i++) {
                        var obj = lst[i];
                        var cssClass = (i % 2) == 0 ? "tableRowImpar" : "tableRowPar";
                        str += "<!-- INICIO: LINHA -->";
                        str += "<tr class='" + (cssClass) + "'>";
                        str += "<td height='25' align='center'><input type='checkbox' name='chkUser' value='" + (obj.Id) + "' item='" + (obj.Name) + "' /></td>";
                        str += "<td>&nbsp;" + (obj.Name) + "</td>";
                        str += "<td>&nbsp;" + (obj.Login) + "</td>";
                        str += "<td>&nbsp;" + (obj.Email) + "</td>";
                        str += "<td>&nbsp;" + (obj.City) + "</td>";
                        str += "<td>";
                        str += "<table border='0' align='center' cellpadding='0' cellspacing='0'>";
                        str += "<tr>";
                        str += "<td><ul id='icons' class='ui-widget ui-helper-clearfix'>";
                        str += "<li class='ui-state-default ui-corner-all' title='Editar registro'><a id='lnkEditar' href='/User/Edit/" + (obj.Id) + "' page='" + page + "'><span class='ui-icon ui-icon-pencil'></span></a></li>";
                        str += "<li class='ui-state-default ui-corner-all' title='Apagar registro'><a id='lnkExcluir' class='addLinkExcluir' id_user=" + (obj.Id) + " page='" + page + "' href='#'><span class='ui-icon ui-icon-close'></span></a></li>";
                        str += "</ul></td>";
                        str += "</tr>";
                        str += "</table>";
                        str += "</td>";
                        str += "</tr>";
                        str += "<!-- FIM: LINHA -->";
                    }
                } else {
                    str += "<tr class='" + "tableRowImpar" + "'>";
                    str += "<td align='center' colspan='6'><br/><span style='color:red;'>NENHUM REGISTRO ENCONTRADO.</span><br/><br/></td>";
                    str += "</tr>";
                }
                str += "<tr>";
                str += "<td height='30' colspan='5' valign='bottom'>";
                str += "<button id='btnApagar' name='btnApagar' class='ui-button ui-state-default ui-corner-all botaoS' title='Apagar selecionados'>";
                str += "Apagar selecionados";
                str += "</button>";
                str += "</td>";
                str += "</tr>";
                str += "</table>";
                str += "<!-- FIM: BLOCO_TABELA -->";

                str += "<!-- INICIO: BLOCO_PAGINACAO -->";
                str += "<table border='0' align='center' cellpadding='0' cellspacing='0'>";
                str += "<tr>";
                str += "<td>";
                str += "<ul id='icons' class='ui-widget ui-helper-clearfix'>";
                str += "<li class='ui-state-default ui-corner-all' title='Primeira página'><a href='#' page='1' class='lnkPagina'><span class='ui-icon ui-icon-seek-first'></span></a></li>";
                str += "<li class='ui-state-default ui-corner-all' title='Voltar página'><a href='#' class='lnkPagina' page=" + (obj != null ? ((response.currpage - 1) > 1 ? (response.currpage - 1) : 1) : 1) + "><span class='ui-icon ui-icon-seek-prev'></span></a></li>";
                str += "</ul>";
                str += "</td>";
                str += "<td class='texto10azulclaro'>";
                if (response != null) {
                    if (response.currpage < 5) {
                        var start = 0;
                        var end = response.totalpages < 7 ? response.totalpages : 7;
                        for (var j = start; j < end; j++) {
                            if ((j + 1) == response.currpage) {
                                if (response.currpage == 1)
                                    str += " | ";
                                str += "<b>[" + (j + 1) + "]</b> | ";
                            }
                            else {
                                if ((j + 1) == 1)
                                    str += " | ";
                                str += "<a href='#' page='" + (j + 1) + "' class='texto10verde lnkPagina'>" + (j + 1) + "</a> | ";
                            }
                        }
                    } else {
                        var start = response.currpage == response.totalpages || (response.currpage + 3) >= response.totalpages ? ((response.currpage - 1) - (6 - (response.totalpages - response.currpage))) : ((response.currpage - 1) - 3);
                        var end = response.currpage == response.totalpages || (response.currpage + 3) >= response.totalpages ? response.totalpages : (response.currpage + 3);
                        for (var j = start; j < end; j++) {
                            if ((j + 1) == response.currpage) {
                                if (response.currpage == start)
                                    str += " | ";
                                str += "<b>[" + (j + 1) + "]</b> | ";
                            }
                            else {
                                if (j == start)
                                    str += " | ";
                                str += "<a href='#' page='" + (j + 1) + "' class='texto10verde lnkPagina'>" + (j + 1) + "</a> | ";
                            }
                        }
                    }

                } else {
                    str += " | <b>[1]</b> | ";
                }
                str += "</td>";
                str += "<td>";
                str += "<ul id='icons' class='ui-widget ui-helper-clearfix'>";
                str += "<li class='ui-state-default ui-corner-all' title='Próximo página'><a href='#' class='lnkPagina' page='" + (obj != null ? ((response.currpage + 1) <= response.totalpages ? (response.currpage + 1) : response.currpage) : 1) + "'><span class='ui-icon ui-icon-seek-next'></span></a></li>";
                str += "<li class='ui-state-default ui-corner-all' title='Última página'><a href='#' class='lnkPagina' page='" + (obj != null ? (response.totalpages) : 1) + "'><span class='ui-icon ui-icon-seek-end'></span></a></li>";
                str += "</ul>";
                str += "</td>";
                str += "</tr>";
                str += "</table>";
                str += "<!-- FIM: BLOCO_PAGINACAO -->";
                $("#divTable").html(str);
            }
        });
    };

    $(".lnkPagina").live('click', function (e) {
        e.preventDefault();
        var page = $(this).attr("page");
        load(page);
    });

    $(".addLinkExcluir").live('click', function (e) {
        e.preventDefault();
        var id = $(this).attr('id_user');
        var page = $(this).attr('page');
        jConfirm("Deseja realmente remover este registro?", "Confirmação", function (r) {
            if (r) {
                $.ajax({
                    type: "POST",
                    url: "/User/Delete",
                    data: { id: id, page: page },
                    dataType: "json",
                    success: function (response) {
                        if (response.Success) {
                            load(page);
                        } else {
                            jAlert("Não foi possível remover o registro selecionado.", "Alerta", function () { return false; });
                        }
                    },
                    error: function (response) {
                        jAlert("Erro", "Erro", function () { return false; });
                    }
                });
            }
        });
    });

    $("#btnListar:first").click(function (e) {
        e.preventDefault();
        var page = $("#hdfPage").val();
        load(page);
    });

    $("#btnApagar:first").live('click', function (e) {
        e.preventDefault();
        var ids = "";
        $("input[type=checkbox][name='chkUser']:checked").each(function () {
            if (ids != "")
                ids += ";";
            ids += $(this).val();
        });

        if (ids != "") {
            $.ajax({
                type: "POST",
                url: "/User/DeleteAll",
                data: { ids: ids },
                dataType: "json",
                success: function (response) {
                    if (response.Success) {
                        var page = $("#hdfPage").val();
                        load(page);
                    } else {
                        jAlert("Não foi possível remover o registro selecionado.", "Alerta", function () { return false; });
                    }
                },
                error: function (response) {
                    jAlert("Erro", "Erro", function () { return false; });
                }
            });
        }
    });

    var page = $("#hdfPage").val();
    load(page);
});